package com.isunland.managesystem.adapter;

import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import butterknife.BindView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProcessContent;
import java.util.ArrayList;

public class WorkProcessAdapter extends BaseButterKnifeAdapter<ProcessContent> {
  private Callback mCallback;

  // Constructors
  public WorkProcessAdapter(BaseVolleyActivity context, ArrayList<ProcessContent> objects,
      Callback callback) {
    super(context, objects);
    this.mCallback = callback;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_file;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override protected void initView(ProcessContent item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    holder.tvFileName.setText(item.getPlanContentDesc());
    holder.tvDate.setText(
        context.getString(R.string.requestFinishTimeWithHolder, item.getPlanEsteTime()));
    holder.ivMore.setTag(item);
    @SuppressWarnings("deprecation") TextDrawable drawable = TextDrawable.builder()
        .beginConfig()
        .fontSize(50)
        .endConfig()
        .buildRound(item.getPlanKindSummary(),
            context.getResources().getColor(item.getPlanKindColor()));
    holder.ivFile.setImageDrawable(drawable);
  }

  private PopupMenu initPopMenu(View view, final ProcessContent itemObj) {
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.inflate(R.menu.menu_distribute_convert_delete);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.menu_item_delete:
            mCallback.delete(itemObj);
            return true;
          case R.id.menu_item_distribute:
            mCallback.distribute(itemObj);
            return true;
          case R.id.menu_item_convert:
            mCallback.convert(itemObj);
            return true;
        }
        return false;
      }
    });
    return popupMenu;
  }

  public interface Callback {
    void delete(ProcessContent item);

    void convert(ProcessContent item);

    void distribute(ProcessContent item);
  }

  protected class ViewHolder extends BaseViewHolder {
    @SuppressWarnings("unused") @BindView(R.id.iv_file) ImageView ivFile;
    @BindView(R.id.tv_fileName) TextView tvFileName;
    @BindView(R.id.tv_date) TextView tvDate;
    @BindView(R.id.iv_more) ImageView ivMore;

    ViewHolder(View view) {
      super(view);
      ivMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (!(v.getTag() instanceof ProcessContent)) return;
          ProcessContent itemObj = (ProcessContent) v.getTag();
          initPopMenu(v, itemObj).show();
        }
      });

    }
  }
}
